package com.light.mapcomponents;

import java.util.ArrayList;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.huw.cameramanagers.CameraStoper;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.Roller;

public class GameMap implements IGameComponents
{
	public static final int PADDING_WIDTH = 180;
	public static final int PADDING_HEIGHT = 120;
	
	protected ArrayList<IGameComponents> m_GameComponentsList = new ArrayList<IGameComponents>();	
		
	protected ArrayList<IGameComponents> m_environmentComponentsList = new ArrayList<IGameComponents>();
	protected ArrayList<PanelComponents> m_panelComponentsList = new ArrayList<PanelComponents>();
	protected Roller m_game;
	
	public ArrayList<IGameComponents> getEvironmentComponentsList() { return m_environmentComponentsList; }
	public ArrayList<PanelComponents> getPanelComponentsList() { return m_panelComponentsList; }
	
	
	public GameMap(Roller game)
	{
		m_game = game;
	}
	
	public void loadMap() {

		m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game,0, 0, 2, Roller.CAMERA_HEIGHT));
		m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game,Roller.CAMERA_WIDTH - 2, 0, 2, Roller.CAMERA_HEIGHT));
		m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game,0, 60, 300, 2));		
		m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game, 0, Roller.CAMERA_HEIGHT - 2, Roller.CAMERA_WIDTH, 2));
		m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game, 0, 0, Roller.CAMERA_WIDTH, 2));		
		m_GameComponentsList.add(MapFactory.CreateCameraStoper(m_game,627, -50, 25, 25));		
		
		m_environmentComponentsList.add(new DoorComponents(m_game, "Door", new Vector2(0, 10), 0.0f));
	}
	
	protected void AutoCreateStoper()
	{
		if(this.m_GameComponentsList.size()>0)
		{
			if(m_GameComponentsList.get(0) instanceof StaticWall)
			{
				int x1=((StaticWall)m_GameComponentsList.get(0)).getX1();
				int x2=((StaticWall)m_GameComponentsList.get(0)).getX2();
				int y1=((StaticWall)m_GameComponentsList.get(0)).getY1();
				int y2=((StaticWall)m_GameComponentsList.get(0)).getY2();
				
				for(int i=1;i<m_GameComponentsList.size();i++)
				{
					if(m_GameComponentsList.get(i) instanceof StaticWall)
					{
						if(x1>((StaticWall)m_GameComponentsList.get(i)).getX1())
							x1=((StaticWall)m_GameComponentsList.get(i)).getX1();
						
						if(x2<((StaticWall)m_GameComponentsList.get(i)).getX2())
							x2=((StaticWall)m_GameComponentsList.get(i)).getX2();
						
						if(y1>((StaticWall)m_GameComponentsList.get(i)).getY1())
							y1=((StaticWall)m_GameComponentsList.get(i)).getY1();
						
						if(y2<((StaticWall)m_GameComponentsList.get(i)).getY2())
							y2=((StaticWall)m_GameComponentsList.get(i)).getY2();
					}
				}
				x1-=PADDING_WIDTH;
				x2+=PADDING_WIDTH;
				y1-=PADDING_HEIGHT;
				y2+=PADDING_HEIGHT;
				
				while(x2-x1<Roller.CAMERA_WIDTH+150)
				{
					x1-=1;
					x2+=1;
				}
				
				while(y2-y1<Roller.CAMERA_HEIGHT+150)
				{
					y1-=1;
					y2+=1;
				}
				
				m_GameComponentsList.add(MapFactory.CreateCameraStoper(m_game, x1, y1, x2-x1, 20));
				m_GameComponentsList.add(MapFactory.CreateCameraStoper(m_game, x1, y1, 20, y2-y1));
				m_GameComponentsList.add(MapFactory.CreateCameraStoper(m_game, x2, y1, 20, y2-y1));
				m_GameComponentsList.add(MapFactory.CreateCameraStoper(m_game, x1, y2, x2-x1, 20));
				
				x1-=PADDING_WIDTH+400;
				x2+=PADDING_WIDTH+400;
				y1-=PADDING_HEIGHT+400;
				y2+=PADDING_HEIGHT+400;
				
				m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game, x1, y1, x2-x1, 20));
				m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game, x1, y1, 20, y2-y1));
				m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game, x2, y1, 20, y2-y1));
				m_GameComponentsList.add(MapFactory.CreateStaticWall(m_game, x1, y2, x2-x1, 20));
			}
		}		
	}	
	
	public void onLoadEngine()
	{				

	}
	
	public void onLoadResources() {	
		loadMap();
		
		for (int i = 0; i < m_environmentComponentsList.size(); i++)
			m_environmentComponentsList.get(i).onLoadResources();
		
		for(int i=0; i< m_GameComponentsList.size();i++)
			m_GameComponentsList.get(i).onLoadResources();
	}
	
	@Override
	public void unLoadResources() {

		for (int i = 0; i < m_environmentComponentsList.size(); i++)
			m_environmentComponentsList.get(i).unLoadResources();		
		
		for(int i=0; i< m_GameComponentsList.size();i++)
			m_GameComponentsList.get(i).unLoadResources();
	}
	
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {

		
		for (int i = 0; i < m_environmentComponentsList.size(); i++)
		{
			m_environmentComponentsList.get(i).onLoadScene(scene, physicsWorld);
			if (m_environmentComponentsList.get(i) instanceof PanelComponents)
				m_panelComponentsList.add((PanelComponents)m_environmentComponentsList.get(i));
		}
		
		for(int i=0; i< m_GameComponentsList.size();i++)
			m_GameComponentsList.get(i).onLoadScene(scene, physicsWorld);		
		
	}


	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {

		
		for (int i = 0; i < m_environmentComponentsList.size(); i++)
			m_environmentComponentsList.get(i).removeFromScene(scene, physicsWorld);		
		
		for(int i=0; i< m_GameComponentsList.size();i++)
			m_environmentComponentsList.get(i).removeFromScene(scene, physicsWorld);	
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
	public void removeStoper(Scene scene, PhysicsWorld physicsWorld) {
	    CameraStoper.clearStoper();
	}
}
